package com.hzp.lolnews.ui.novel.view;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hzp.lolnews.R;
import com.hzp.lolnews.db.ShopDao;
import com.hzp.lolnews.ui.novel.other.ShareListener;

public class DetailShopActivity extends AppCompatActivity implements View.OnClickListener{

    private SimpleDraweeView iv_pic;
    private TextView tv_type_name;
    private TextView tv_format_date;
    private TextView tv_title;
    private TextView tv_price;
    private Toolbar mToolbar;
    private String getArticle_price;
    private String getArticle_title;
    private ShopDao shopDao;
    private String getArticle_pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_detail_shop);
        //初始化控件
        initView();
        //获取数据
        getData();
    }

    private void initView() {
        shopDao = new ShopDao(this);
        mToolbar = (Toolbar) findViewById(R.id.detailShop_toolbar);
        iv_pic = (SimpleDraweeView) findViewById(R.id.detailShop_pic);
        tv_type_name = (TextView) findViewById(R.id.detailShop_type_name);
        tv_price = (TextView) findViewById(R.id.detailShop_price);
        tv_format_date = (TextView) findViewById(R.id.detailShop_format_date);
        tv_title = (TextView) findViewById(R.id.detailShop_title);
        findViewById(R.id.detailShop_share).setOnClickListener(this);
        findViewById(R.id.detailShop_collect).setOnClickListener(this);
        findViewById(R.id.detailShop_buy).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.detailShop_share://分享
                Toast.makeText(DetailShopActivity.this,"分享了",Toast.LENGTH_LONG).show();
                ShareListener.shareListener(DetailShopActivity.this,getArticle_title,getArticle_price);
                break;
            case R.id.detailShop_collect://收藏
                String sql = "select * from collect where title = ?";
                Cursor select = shopDao.select(sql, new String[]{getArticle_title});
                if (select != null) {
                    if (select.moveToNext()) {
                        int columnIndex = select.getColumnIndex("title");
                        String title = select.getString(columnIndex);
                        String whereClause = "title = ?";
                        shopDao.delete("collect", whereClause, new String[]{getArticle_title});
                        Toast.makeText(DetailShopActivity.this, "取消收藏!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                ContentValues values = new ContentValues();
                values.put("name", getArticle_pic);
                values.put("title", getArticle_title);
                long insert = shopDao.insert("collect", values);
                if (insert > 0) {
                    Toast.makeText(DetailShopActivity.this, "收藏成功!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.detailShop_buy://购买
                Toast.makeText(DetailShopActivity.this,"功能未开启",Toast.LENGTH_LONG).show();
                break;
        }
    }

    public void getData() {
        Intent intent = getIntent();
        getArticle_pic = intent.getStringExtra("getArticle_pic");
        String getArticle_type_name = intent.getStringExtra("getArticle_type_name");
        String getArticle_format_date = intent.getStringExtra("getArticle_format_date");
        getArticle_title = intent.getStringExtra("getArticle_title");
        getArticle_price = intent.getStringExtra("getArticle_price");
        iv_pic.setImageURI(Uri.parse(getArticle_pic));
        tv_title.setText(getArticle_type_name);
        tv_format_date.setText(getArticle_format_date);
        tv_title.setText(getArticle_title);
        tv_price.setText(getArticle_price);
        String sql = "select * from history where title =?";
        Cursor select = shopDao.select(sql, new String[]{getArticle_title});
        if (select != null) {
            if (!select.moveToNext()) {
                ContentValues values = new ContentValues();
                values.put("name", getArticle_pic);
                values.put("title", getArticle_title);
                shopDao.insert("history", values);
            }
        }

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
